package com.situ.stmall.admin.mapper;

import com.situ.stmall.admin.entity.GoodsSort;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;
@Mapper
public interface GoodsSortMapper {
    @Delete({
        "delete from goods_sort",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into goods_sort ( name, ",
        "picture, parent) ",
        
        "values (#{name,jdbcType=VARCHAR}, ",
        "#{picture,jdbcType=VARCHAR}, #{parent,jdbcType=INTEGER})"
        
    })
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    int insert(GoodsSort record);

    @Select({
        "select",
        "id, name, picture, parent, status",
        "from goods_sort",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="picture", property="picture", jdbcType=JdbcType.VARCHAR),
        @Result(column="parent", property="parent", jdbcType=JdbcType.INTEGER),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
        @Result(column = "parent",property = "parentSort", 
        		one = @One(select = "selectByPrimaryKey"))
    })
    GoodsSort selectByPrimaryKey(Integer id);

    @Select({
        "select",
        "id, name, picture, parent, status",
        "from goods_sort"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="picture", property="picture", jdbcType=JdbcType.VARCHAR),
        @Result(column="parent", property="parent", jdbcType=JdbcType.INTEGER),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER)
    })
    List<GoodsSort> selectAll();

    @Update({
    	"<script>",
        "update goods_sort",
        "<set>",
        	"<if test='name!=null'>name = #{name},</if>",
        	"<if test='picture!=null'>picture = #{picture},</if>",
        	"<if test='parent==null'>parent = null,</if>",
        	"<if test='parent!=null'>parent = #{parent},</if>",
        	"<if test='status!=null'>status = #{status},</if>",
        "</set>",
        "where id = #{id,jdbcType=INTEGER}",
         "</script>"
    })
    int updateByPrimaryKey(GoodsSort record);
    
    @Select({
    	"<script>",
    	"select * from goods_sort",
    	"<where>",
    	"<if test='name!=null'>name like '%${name}%'</if>",
    	"<if test='parent==null'>and parent is null</if>",
    	"<if test='parent!=null and parent!=0'>and parent = #{parent}</if>",
    	"<if test='status!=null'>and status = #{status}</if>",
    	"</where>",
    	"</script>"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="picture", property="picture", jdbcType=JdbcType.VARCHAR),
        @Result(column="parent", property="parent", jdbcType=JdbcType.INTEGER),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
        @Result(column="parent", property ="parentSort",
        		one = @One(select = "selectByPrimaryKey"))
    })
    List<GoodsSort> select(GoodsSort goodsSort);
    
    
}